<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/2/21
 * Time: 14:51
 */

namespace iBrand\Member\Backend\Repository;


use iBrand\Member\Backend\Models\VipPlanMember;
use Prettus\Repository\Eloquent\BaseRepository;

class VipMemberRepository extends BaseRepository
{
    public function model()
    {
        return VipPlanMember::class;
    }

    public function getMemberPaginate($where, $time, $mobile, $limit = 15)
    {
        return $this->scopeQuery(function ($query) use ($where, $time, $mobile) {
            $query = $query->Where(function ($query) use ($where) {
                if (count($where)) {
                    foreach ($where as $key => $value) {
                        if (is_array($value)) {
                            list($operate, $va) = $value;
                            $query = $query->where($key, $operate, $va);
                        } else {
                            $query = $query->where($key, $value);
                        }
                    }
                }
            });

            if (count($time)) {
                $query = $query->where(function ($query) use ($time) {
                    foreach ($time as $key => $value) {
                        if (is_array($value)) {
                            list($operate, $va) = $value;
                            $query = $query->where($key, $operate, $va);
                        } else {
                            $query = $query->where($key, $value);
                        }
                    }

                });
            }

            if (!empty($mobile)) {
                $query = $query->whereHas('user', function ($query) use ($mobile) {
                    return $query->where('mobile', 'like', '%' . $mobile . '%');
                });
            }

            return $query->with(['user', 'order', 'plan'])->orderBy('created_at', 'desc');
        })->paginate($limit);
    }

}